package jsyf.oa.web.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.entity.system.DepartEntity;
import jsyf.oa.facade.system.DepartFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/12/7 0007.
 */
@Api(value = "部门接口", description = "部门接口")
@Controller
@RequestMapping("/depart")
public class DepartController {


    @Resource
    private DepartFacade departFacade;

    /**
     * @param response
     * @param jsonData
     * @Description: 保存/修改部门 <br/>
     * @return: void
     * @throw:
     * @author: 崔程鵬
     * @createTime: 2017/11/27 0027 10:18
     * @Version: V1.0.0
     */
    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.departFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }


    /**
     * @param response
     * @param jsonData
     * @Description: 根据ID逻辑删除部门  <br/>
     * @return: void
     * @throw:
     * @author: 崔程鵬
     * @createTime: 2017/11/24 0024 15:14
     * @Version: V1.0.0
     */
    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        DepartEntity departEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DepartEntity.class);
        String id = departEntity.getId();
        String result = this.departFacade.delById(id);
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/sort.json", method = RequestMethod.POST)
    public void sort(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean result = departFacade.sort(jsonParam.getString("departIds"));
        JSONObject objResult = new JSONObject();
        objResult.put("success",result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }
}
